/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { Space, Popconfirm, message } from 'antd';
import KbBlockModal from '../KbBlockModal/';
import type { KbSwitchType } from '../../index.d';
import { upDataUserInfo } from '@/services/yz/manage';

interface SwitchType {
  is_login: {
    open: string;
    close: string;
  };
  is_pass: {
    open: string;
    close: string;
  };
  is_withdraw: {
    open: string;
    close: string;
  };
}

const KbSwitch = ({ type, record, actionRef }: KbSwitchType) => {
  const isPass = type === 'is_pass';
  const queryUrl = isPass ? '/Inn/innAdminEdit' : '/Inn/blackInnSet';
  const switchType: SwitchType = {
    is_login: {
      close: '是否要将此驿站禁止登录',
      open: '是否要将此驿站开启登录',
    },
    is_pass: {
      close: '关闭后，该驿站将不再有入库权限，但是不影响登录和其他操作。',
      open: '您确定要开启驿站吗？',
    },
    is_withdraw: {
      close: '是否要将此驿站禁止提现',
      open: '是否要将此驿站开启提现',
    },
  };

  const getStatusType = () => (isPass ? record[type] == 0 || record[type] == 2 : record[type] == 0);

  const handleSwitch = (params: { remark?: string }) => {
    const { dak_id, phone } = record;
    const { remark } = params;
    const _type = getStatusType();
    const queryData = {
      [type]: _type ? 1 : isPass ? 2 : 0,
      cm_id: dak_id,
      phone,
      remark,
    };
    upDataUserInfo(queryData, queryUrl).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        actionRef?.current?.reload();
        if (remark) {
          return new Promise<void>((resolve) => {
            resolve(res);
          });
        }
        message.success(msg);
        return;
      } else {
        message.error(msg);
        return;
      }
    });
  };

  return (
    <Space>
      {getStatusType() ? (
        <Space direction='vertical'>
          <span>已关闭</span>
          <Popconfirm
            title={`${switchType[type][getStatusType() ? 'open' : 'close']}`}
            onConfirm={handleSwitch.bind(null, {})}
          >
            <a>开启</a>
          </Popconfirm>
        </Space>
      ) : (
        <Space direction='vertical'>
          <span>已开启</span>
          <KbBlockModal
            formTitle='确定将此驿站禁止/登录/入库/提现？'
            item={record}
            callBack={handleSwitch}
          >
            <a>关闭</a>
          </KbBlockModal>
        </Space>
      )}
    </Space>
  );
};

export default KbSwitch;
